//
//  ModifyInfomationView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/21.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class ModifyInfomationView: UIView {
    
    // 输入
    private let input = TextFieldOffsetPlaceHolder()
    // 标题
    private let label = UILabel(title: "", color: COLOR_NICE, size: 17)
    // 回调
    public var complete:((_ item:NSDictionary,_ isEmpty:Bool) -> Void)?
    
    public var resetLabel:String? {
        didSet {
            label.text = resetLabel
        }
    }
    
    public var resetContent:String? {
        didSet {
            input.text = resetContent
        }
    }
    
    override func layoutSubviews() {
        let back = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height: MARGIN70))
        back.backgroundColor = UIColor.white
        // 标题
        label.frame = CGRect(x: MARGIN, y: 0, width: MARGIN * 4.8, height: MARGIN70)
        back.addSubview(label)
        // 输入
        input.frame = CGRect(x: MARGIN, y: 0, width: frame.width, height: MARGIN70)
        // 性别
        if let gender = label.text,gender.elementsEqual(GENDER) {
            // 请选择文本
            let plsSelect = UILabel(title: "请选择", color: UIColor.lightGray, size: 13, align: .right)
            plsSelect.isUserInteractionEnabled = false
            let plsSelectX = frame.width - MARGIN100
            plsSelect.frame = CGRect(x: plsSelectX, y: 0, width: MARGIN70, height: MARGIN70)
            back.addSubview(plsSelect)
            // 箭头
            let arrowView = UIImageView(image: UIImage(named: "arrow_6x11"))
            let arrowX = frame.width - arrowView.bounds.width - MARGIN
            let arrowY = (MARGIN70 - arrowView.bounds.height) * 0.5
            arrowView.frame.origin = CGPoint(x: arrowX, y: arrowY)
            back.addSubview(arrowView)
            // 添加点击事件
            input.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickGender)))
        }
        input.textColor = UIColor.lightGray
        input.keyboardType = .asciiCapable
        input.font = UIFont.systemFont(ofSize: 13)
        input.setValue(MARGIN * 5, forKey: "paddingLeft")
        back.addSubview(input)
        addSubview(back)
        // 确认修改按钮
        let saveBtn = UIButton(title: "确认修改", name: "button_orange",radius: 5)
        let saveBtnY = input.frame.maxY + MARGIN35
        saveBtn.frame = CGRect(x: MARGIN, y: saveBtnY, width: frame.width - MARGIN * 2, height: MARGIN50)
        addSubview(saveBtn)
        // 点击事件
        saveBtn.addTarget(self, action: #selector(clickModifyInfo), for: .touchUpInside)
    }
    
    @objc func clickModifyInfo() -> Void {
        if let callback = complete {
            if input.text?.count == 0 {
                callback(NSDictionary(),true)
                return
            }
            // 拼接数据
            guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
                return
            }
            let dict = NSMutableDictionary()
            dict.setValue(userid, forKey: SP_USERID)
            // 区分不同标签
            switch resetLabel {
            case "邮箱":
                dict.setValue(input.text, forKey: SP_EMAIL)
            case "性别":
                dict.setValue(input.text, forKey: SP_GENDER)
            case "手机号":
                dict.setValue(input.text, forKey: SP_MOBILE)
            case "姓名":
                dict.setValue(input.text, forKey: SP_USERNAME)
            case "身份证号":
                dict.setValue(input.text, forKey: SP_IDNUMBER)
            default:
                break
            }
            callback(dict,false)
        }
    }
    
    @objc func clickGender() -> Void {
        input.text = input.text!.elementsEqual(NAN) ? NV : NAN
    }
}
